<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <form name="form">
      <p>
        <input type="text" name="input" />
      </p>
    </form>
    <script>
      window.addEventListener("focusin", () => {
        console.log("window focusin");
      });
      window.addEventListener("focusout", () => {
        console.log("window focusout");
      });

      document.form.input.addEventListener("focusin", () => {
        console.log("focusin");
      });
      document.form.input.addEventListener("focusout", () => {
        console.log("focusout");
      });
      setTimeout(() => {
        // 获取焦点触发获取焦点事件
        document.form.input.focus();
      }, 3000);

      setTimeout(() => {
        // 取消焦点触发失去焦点事件
        document.form.input.blur();
      }, 6000);
    </script>
  </body>
</html>
